package com.partsoft.utils;

import java.util.Arrays;

public abstract class CompareUtils {
	
	public static boolean nullSafeEquals(Object o1, Object o2) {
		if (o1 == o2) {
			return true;
		}
		if ((o1 == null) || (o2 == null)) {
			return false;
		}
		if (o1.equals(o2)) {
			return true;
		}
		if ((o1.getClass().isArray()) && (o2.getClass().isArray())) {
			if ((o1 instanceof Object[]) && (o2 instanceof Object[])) {
				return Arrays.equals((Object[]) o1, (Object[]) o2);
			}
			if ((o1 instanceof boolean[]) && (o2 instanceof boolean[])) {
				return Arrays.equals((boolean[]) o1, (boolean[]) o2);
			}
			if ((o1 instanceof byte[]) && (o2 instanceof byte[])) {
				return Arrays.equals((byte[]) o1, (byte[]) o2);
			}
			if ((o1 instanceof char[]) && (o2 instanceof char[])) {
				return Arrays.equals((char[]) o1, (char[]) o2);
			}
			if ((o1 instanceof double[]) && (o2 instanceof double[])) {
				return Arrays.equals((double[]) o1, (double[]) o2);
			}
			if ((o1 instanceof float[]) && (o2 instanceof float[])) {
				return Arrays.equals((float[]) o1, (float[]) o2);
			}
			if ((o1 instanceof int[]) && (o2 instanceof int[])) {
				return Arrays.equals((int[]) o1, (int[]) o2);
			}
			if ((o1 instanceof long[]) && (o2 instanceof long[])) {
				return Arrays.equals((long[]) o1, (long[]) o2);
			}
			if ((o1 instanceof short[]) && (o2 instanceof short[])) {
				return Arrays.equals((short[]) o1, (short[]) o2);
			}
		}
		return false;
	}

	public static boolean stringEquals(String s1, String s2, boolean nullable) {
		if ((s1 != null) && (s1.length() > 0)) {
			if ((s2 == null) || (s2.length() <= 0)) {
				return false;
			}
			return s2.equals(s1);
		}
		return (s2 == null) || (s2.length() <= 0);
	}

	public static boolean stringIgnoreCaseEquals(String s1, String s2, boolean nullable) {
		if ((s1 != null) && (s1.length() > 0)) {
			if ((s2 == null) || (s2.length() <= 0)) {
				return false;
			}
			return s2.equalsIgnoreCase(s1);
		}
		return (s2 == null) || (s2.length() <= 0);
	}

	public static boolean bytesEquals(byte[] b1, byte[] b2, boolean nullable) {
		if ((b1 != null) && (b1.length > 0)) {
			if ((b2 == null) || (b2.length <= 0)) {
				return false;
			}

			for (int i = 0; (i < b1.length) && (i < b2.length); i++) {
				if (b1[i] != b2[i]) {
					return false;
				}
			}
			return b1.length == b2.length;
		}

		return (b2 == null) || (b2.length <= 0);
	}

	public static boolean bytesEquals(byte[] b1, byte[] b2, int length, boolean nullable) {
		if ((b1 != null) && (b1.length > 0)) {
			if ((b2 == null) || (b2.length <= 0)) {
				return false;
			}

			for (int i = 0; (i < length) && (i < b1.length) && (i < b2.length); i++) {
				if (b1[i] != b2[i])
					return false;
			}

			if ((length <= b1.length) && (length <= b2.length)) {
				return length <= 0 ? nullable : true;
			}
			return b1.length == b2.length;
		}

		return (b2 == null) || (b2.length <= 0) ? nullable : false;
	}

	public static int bytesCompare(byte[] b1, byte[] b2) {
		int length1 = b1 == null ? 0 : b1.length;
		int length2 = b2 == null ? 0 : b2.length;

		if ((length1 == length2) && (length1 > 0)) {
			for (int i = 0; i < length1; i++) {
				int result = cpl((b1[i] & 0xFF) - (b2[i] & 0xFF));
				if (result != 0)
					return result;
			}
			return 0;
		}

		return cpl(length1 - length2);
	}

	public static int stringCompare(String b1, String b2) {
		int length1 = b1 == null ? 0 : b1.length();
		int length2 = b2 == null ? 0 : b2.length();

		if ((length1 == length2) && (length1 > 0)) {
			for (int i = 0; i < length1; i++) {
				int result = cpl(b1.charAt(i) - b2.charAt(i));

				if (result != 0)
					return result;
			}
			return 0;
		}

		return cpl(length1 - length2);
	}

	private static int cpl(int value) {
		if (value < 0) {
			return -1;
		}
		if (value > 0) {
			return 1;
		}
		return 0;
	}

}